﻿using BitConverter;
using System;

namespace JianZhangTool.net
{
    class BiliDanmuPacket
    {
        /// <summary>
        /// 消息总长度 (协议头+数据长度)
        /// </summary>
        public int length;
        /// <summary>
        /// 消息头长度 (固定为16)
        /// </summary>
        public short header_length;
        /// <summary>
        /// 消息版本号
        /// </summary>
        public short version;
        /// <summary>
        /// 消息类型
        /// </summary>
        public int action;
        /// <summary>
        /// 参数, 固定为1
        /// </summary>
        public int param;

        /// <summary>
        /// 解包
        /// </summary>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static BiliDanmuPacket FromBuffer(byte[] buffer)
        {
            if (buffer.Length < 16) 
            { 
                throw new ArgumentException(); 
            }
            return new BiliDanmuPacket()
            {
                length = EndianBitConverter.BigEndian.ToInt32(buffer, 0),
                header_length = EndianBitConverter.BigEndian.ToInt16(buffer, 4),
                version = EndianBitConverter.BigEndian.ToInt16(buffer, 6),
                action = EndianBitConverter.BigEndian.ToInt32(buffer, 8),
                param = EndianBitConverter.BigEndian.ToInt32(buffer, 12),
            };
        }
    }
}
